﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLCH_ANUONG.Forms
{
    public partial class frmLichLamViec : Form
    {
        public frmLichLamViec()
        {
            InitializeComponent();
            string strTag = Convert.ToString(this.Tag);
           // clsMain.flexConn.InitForms(strTag, ref fclsvNhanVien);
        }
        private string strNVCode = "", strCaLamViecCode = "", strSQL = "";
        private int intDay = 0, intThu = 0,intMonth=0;
        private void frmLichLamViec_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(System.Windows.Forms.Keys.Escape))
            {
                this.Visible = false;
            }
        }

        private void fclsvNhanVien_DoubleClick(object sender, EventArgs e)
        {

            RefreshCheckBox();
        }
        private void LoadCaLamViec(Label lbl,ref FlexControls.fcListView fcLsv)
        {
            try
            {
                string strCheck = "", strUnCheck = "";
                string strValue = Convert.ToString(lbl.Text);
                char[] delimiters = new char[] { ' ', '-' };
                string[] strCode = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                //mm/dd/yyyy
                string strNgay = strCode[0] + "/" + strCode[1] + "/" + dateTimePicker.Value.Year;
                DateTime Ngay = Convert.ToDateTime(strNgay);
                strNgay = Ngay.ToString("yyyy/MM/dd");
                strCheck = "exec proc_load_chitietlamviec " + "'" + fclsvNhanVien.strSelectItemCode + "','" + strNgay + "'";
                strUnCheck = "exec proc_load_calamviec";
                fcLsv.LoadDataWithCheckBox(strCheck, strUnCheck, clsMain.flexConn.fcConn);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Load Ca Lam Viec \n" + ex.ToString());
            }
        }
        private void fclsvCaLamViec_ItemChecked(object sender, EventArgs e)
        {

            SaveCaLamViec(ref fclsvCaLamViecThu2, label2);

        }
        private void SaveCaLamViec(ref FlexControls.fcListView fcLsv, Label lbl)
        {
            try
            {
                string strValue = Convert.ToString(lbl.Text);
                char[] delimiters = new char[] { ' ', '-' };
                string[] strCode = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                //mm/dd/yyyy
                string strNgay = strCode[0] + "/" + strCode[1] + "/" + dateTimePicker.Value.Year;
                DateTime Ngay = Convert.ToDateTime(strNgay);
                strNgay = Ngay.ToString("yyyy/MM/dd");
                strCaLamViecCode = ",'" + fcLsv.strItemCheckTag + "' ";
                if (fcLsv.booItemChecked == true)
                {
                    strSQL = "set dateformat dmy exec proc_save_lichlamviec " + "1,";
               }
                else
                {
                    strSQL = "set dateformat dmy exec proc_save_lichlamviec " + "3,";
                }
                strSQL += strNVCode + strCaLamViecCode + ",'" + strNgay +"'";
                clsMain.flexConn.ExcuteSQL(strSQL, clsMain.flexConn.fcConn);
                strNgay = Ngay.ToString("dd/MM/yyyy");
                if(strNgay==dateTimePicker.Text.ToString())
                ReFreshLichLamViec();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Save Ca Lam Viec\n" + ex.ToString());
            }
        }
        private void fctxtNgay_DoubleClick(object sender, EventArgs e)
        {
            fclsvCaLamViecThu2.lsv.Clear();
            ReFreshLichLamViec();
            
        }

        private void frmLichLamViec_Load(object sender, EventArgs e)
        {

            try 
            {
                Color lblColor = System.Drawing.Color.Blue;
                float lblFontSize = 10;
                label2.ForeColor = lblColor;
                label3.ForeColor = lblColor;
                label4.ForeColor = lblColor;
                label5.ForeColor = lblColor;
                label6.ForeColor = lblColor;
                label7.ForeColor = lblColor;
                label8.ForeColor = lblColor;
                Font fontTemp = new Font("Tahoma", lblFontSize);
                label2.Font = fontTemp;
                label3.Font = fontTemp;
                label4.Font = fontTemp;
                label5.Font = fontTemp;
                label6.Font = fontTemp;
                label7.Font = fontTemp;
                label8.Font = fontTemp;
                lblColor = System.Drawing.Color.Red;
                fontTemp =new Font("Tahoma", 12);
                string strThu = "";
                
                strThu = dateTimePicker.Value.DayOfWeek.ToString();
                intMonth = dateTimePicker.Value.Month;
                switch (strThu)
                {
                    case "Monday":
                        intThu = 1;

                        break;
                    case "Tuesday":
                        intThu = 2;

                        break;
                    case "Wednesday":
                        intThu = 3;

                        break;
                    case "Thursday":
                        intThu = 4;
                        
                        break;
                    case "Friday":
                        intThu = 5;

                        break;
                    case "Saturday":
                        intThu = 6;

                        break;
                    case "Sunday":
                        intThu = 7;
                        label8.ForeColor = lblColor;
                        label8.Font = fontTemp;
                        break;
                    default:
                        break;
                }
           //     this.proc_load_lichlamviec_theongayTableAdapter3.Fill(this.test1.proc_load_lichlamviec_theongay);

                intDay = Convert.ToInt32(dateTimePicker.Value.Day) - intThu + 1;

                setLabelDate(ref label2, intDay);
                
                setLabelDate(ref label3, intDay  );

                setLabelDate(ref label4, intDay );

                setLabelDate(ref label5, intDay );

                setLabelDate(ref label6, intDay  );

                setLabelDate(ref label7, intDay  );

                setLabelDate(ref label8, intDay );
                lblStatus.Text = "Lịch làm việc của nhân viên: chưa chọn nhân viên";
              //  RefreshCheckBox();
               // KhoiTaoInterface();

            }
            catch(Exception ex)
            {
                MessageBox.Show("Load\n" + ex.ToString());
            }

        }
        private int intTop = 10;
        private int intLeft = 200;
        
       
        private Label[] lblTenCa = new Label[50];
        private Label[] lblNhanVien = new Label[50];
        private CheckBox[] chkCa = new CheckBox[50];
        private int intLabel = 0;
        private int intNhanVien = 0;
        private int XNhanVien=10, YNhanVien=10;
        private void KhoiTaoInterface()
        {
            try
            {
                
                int X = intLeft, Y = intTop;
                
                string strSQL = "select * from ca_lamviec";
                ADODB.Recordset Rs = new ADODB.Recordset();
                ADODB.Recordset RsNhanVien = new ADODB.Recordset();
                Rs.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                strSQL = "select CODE,DATA from Nhan_Vien";
                RsNhanVien.Open(strSQL, clsMain.flexConn.fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                if (RsNhanVien.RecordCount > 0)
                {
                    for (int j = 0; j < RsNhanVien.RecordCount - 1; j++)
                    {
                        lblNhanVien[intNhanVien] = new Label();
                        lblNhanVien[intNhanVien].AutoSize = true;
                        lblNhanVien[intNhanVien].BackColor = System.Drawing.Color.Transparent;
                        lblNhanVien[intNhanVien].Visible = true;
                        lblNhanVien[intNhanVien].Enabled = true;
                        lblNhanVien[intNhanVien].Location = new Point(XNhanVien, YNhanVien);
                        lblNhanVien[intNhanVien].Tag = RsNhanVien.Fields[0].Value;
                      //  xtraTabPage2.Controls.Add(lblNhanVien[intNhanVien]);
                        lblNhanVien[intNhanVien].Show();
                        lblNhanVien[intNhanVien].Text = RsNhanVien.Fields[0].Value + ": " + RsNhanVien.Fields[1].Value;
                        intNhanVien++;
                        if (Rs.RecordCount > 0)
                        {
                            for (int i = 0; i < Rs.RecordCount - 1; i++)
                            {
                                lblTenCa[intLabel] = new Label();
                                chkCa[intLabel] = new CheckBox();
                                if (intLabel > 0)
                                {
                                    X += lblTenCa[intLabel - 1].Size.Width + 20;
                                }

                                lblTenCa[intLabel].AutoSize = true;
                                lblTenCa[intLabel].BackColor = System.Drawing.Color.Transparent;
                                lblTenCa[intLabel].Text = Rs.Fields[1].Value;
                                lblTenCa[intLabel].Visible = true;
                                lblTenCa[intLabel].Enabled = true;
                                lblTenCa[intLabel].Location = new Point(X, Y);

                                chkCa[intLabel].AutoSize = true;
                                chkCa[intLabel].Text = "";
                                chkCa[intLabel].Checked = true;
                                chkCa[intLabel].Tag = Rs.Fields[0].Value;
                                chkCa[intLabel].Location = new Point(X + 5, Y + 13);

                          //      xtraTabPage2.Controls.Add(chkCa[intLabel]);
                               // xtraTabPage2.Controls.Add(lblTenCa[intLabel]);
                                chkCa[intLabel].Show();
                                lblTenCa[intLabel].Show();
                                intLabel++;
                                Rs.MoveNext();
                            }
                            Rs.MoveFirst();

                        }
                        Y += 18;
                        YNhanVien += 18;
                        X = intLeft;
                        RsNhanVien.MoveNext();
                    }
                    RsNhanVien.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("KhoiTaoInterface\n" + ex.ToString());
            }

        }
        public void setLabelDate(ref Label lblDate, int intNgay)
        {
            try 
            {
                int temp = dateTimePicker.Value.Date.Month;
                if (intNgay <= 0)
                {
                    temp = DateTime.DaysInMonth(dateTimePicker.Value.Year, intMonth - 1);
                    intMonth--;
                    
                    intNgay += temp;
                    intDay = intNgay;
                }
                else if (intNgay > DateTime.DaysInMonth(dateTimePicker.Value.Year, intMonth ))
                {
                    intDay = 1;
                    intNgay = 1;
                    intMonth++;
                }
                if (intNgay == dateTimePicker.Value.Day && intMonth==dateTimePicker.Value.Month)
                {
                    Color lblColor = System.Drawing.Color.Red;
                    Font fontTemp = new Font("Tahoma", 12);
                    lblDate.ForeColor = lblColor;
                    lblDate.Font = fontTemp;
                }
                else
                {
                    Color lblColor = System.Drawing.Color.Blue;
                    Font fontTemp = new Font("Tahoma", 10);
                    lblDate.ForeColor = lblColor;
                    lblDate.Font = fontTemp;
                }
                lblDate.Text = Convert.ToString(intNgay) + " - " +intMonth;
                intDay++;
            }
            catch (Exception ex)
            {
                MessageBox.Show("set label date\n" + ex.ToString());
            }
        }
        private void fclsvNhanVien_Load(object sender, EventArgs e)
        {
            DateTime Ngay = (dateTimePicker.Value);
            string strNgay = Ngay.ToString("yyyy/MM/dd");
            fclsvNhanVien.LoadData("exec proc_load_lichlamviec '" + strNgay +"'", clsMain.flexConn.fcConn);
            //fclsvNhanVien_TheoNgay.LoadData("exec proc_load_lichlamviec_theongay '" + strNgay + "'", clsMain.flexConn.fcConn);

         //   fclsvNhanVien.LoadData("exec proc_load_lichlamviec '2013/03/01'", clsMain.flexConn.fcConn);
        }
        private void ReFreshLichLamViec()
        {
            DateTime Ngay = Convert.ToDateTime(dateTimePicker.Text);
            string strNgay = Ngay.ToString("yyyy/MM/dd");
            fclsvNhanVien.LoadData("exec proc_load_lichlamviec '" + strNgay + "'", clsMain.flexConn.fcConn);
           // fclsvNhanVien_TheoNgay.LoadData("exec proc_load_lichlamviec '" + strNgay + "'", clsMain.flexConn.fcConn);
        }

        private void dateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            ReFreshLichLamViec();
        }

        private void butPrev_Click(object sender, EventArgs e)
        {
            intDay -=14;

            setLabelDate(ref label2, intDay);

            setLabelDate(ref label3, intDay );

            setLabelDate(ref label4, intDay );

            setLabelDate(ref label5, intDay );

            setLabelDate(ref label6, intDay );

            setLabelDate(ref label7, intDay );

            setLabelDate(ref label8, intDay );
            RefreshCheckBox();

        }

        private void butNext_Click(object sender, EventArgs e)
        {


            setLabelDate(ref label2, intDay);

            setLabelDate(ref label3, intDay);

            setLabelDate(ref label4, intDay );

            setLabelDate(ref label5, intDay) ;

            setLabelDate(ref label6, intDay );

            setLabelDate(ref label7, intDay );

            setLabelDate(ref label8, intDay );
            RefreshCheckBox();
            
        }
        private void RefreshCheckBox()
        {
            try
            {
                // fclsvCaLamViecThu2.LoadData("exec proc_load_chitietlamviec " + "'" + fclsvNhanVien.strSelectItemCode + "','" + dateTimePicker.Text + "'",clsMain.flexConn.fcConn);
                string strCheck = "", strUnCheck = "";
                string strValue = Convert.ToString(label2.Text);
                char[] delimiters = new char[] { ' ', '-' };
                string[] strCode = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                //mm/dd/yyyy
                string strNgay = strCode[0] + "/" + strCode[1] + "/" + dateTimePicker.Value.Year;
                DateTime Ngay = Convert.ToDateTime(strNgay);
                strNgay = Ngay.ToString("yyyy/MM/dd");
                strCheck = "exec proc_load_chitietlamviec " + "'" + fclsvNhanVien.strSelectItemCode + "','" + strNgay + "'";
                strUnCheck = "exec proc_load_calamviec";
                fclsvCaLamViecThu2.LoadDataWithCheckBox(strCheck, strUnCheck, clsMain.flexConn.fcConn);

                strNVCode = "'" + fclsvNhanVien.strSelectItemCode + "' ";
                LoadCaLamViec(label3, ref fclsvCaLamViecThu3);
                LoadCaLamViec(label4, ref fclsvCaLamViecThu4);
                LoadCaLamViec(label5, ref fclsvCaLamViecThu5);
                LoadCaLamViec(label6, ref fclsvCaLamViecThu6);
                LoadCaLamViec(label7, ref fclsvCaLamViecThu7);
                LoadCaLamViec(label8, ref fclsvCaLamViecThu8);
                lblStatus.Text = "Lịch làm việc của nhân viên: " + fclsvNhanVien.strSelectItemData;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Refresh CheckBox\n" + ex.ToString());
            }
        }
        private void fclsvCaLamViecThu3_ItemChecked(object sender, EventArgs e)
        {
            SaveCaLamViec(ref fclsvCaLamViecThu3, label3);
        }

        private void fclsvCaLamViecThu4_ItemChecked(object sender, EventArgs e)
        {
            SaveCaLamViec(ref fclsvCaLamViecThu4, label4);
        }

        private void fclsvCaLamViecThu5_ItemChecked(object sender, EventArgs e)
        {
            SaveCaLamViec(ref fclsvCaLamViecThu5, label5);
        }

        private void fclsvCaLamViecThu6_ItemChecked(object sender, EventArgs e)
        {
            SaveCaLamViec(ref fclsvCaLamViecThu6, label6);
        }

        private void fclsvCaLamViecThu7_ItemChecked(object sender, EventArgs e)
        {
            SaveCaLamViec(ref fclsvCaLamViecThu7, label7);
        }

        private void fclsvCaLamViecThu8_ItemChecked(object sender, EventArgs e)
        {
            SaveCaLamViec(ref fclsvCaLamViecThu8, label8);
        }

        private void fclsvNhanVien_Click(object sender, EventArgs e)
        {
            string strCheck = "", strUnCheck = "";



            string strValue = Convert.ToString(label2.Text);
            char[] delimiters = new char[] { ' ', '-' };
            string[] strCode = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            //mm/dd/yyyy
            string strNgay = strCode[1] + "/" + strCode[0] + "/" + dateTimePicker.Value.Year;
            DateTime Ngay = Convert.ToDateTime(strNgay);
            strNgay = Ngay.ToString("yyyy/MM/dd");
            strCheck = "exec proc_load_chitietlamviec " + "'" + fclsvNhanVien.strSelectItemCode + "','" + strNgay + "'";
            strUnCheck = "exec proc_load_calamviec";
            fclsvCaLamViecThu2.LoadDataWithCheckBox(strCheck, strUnCheck, clsMain.flexConn.fcConn);

            strNVCode = "'" + fclsvNhanVien.strSelectItemCode + "' ";
            LoadCaLamViec(label3, ref fclsvCaLamViecThu3);
            LoadCaLamViec(label4, ref fclsvCaLamViecThu4);
            LoadCaLamViec(label5, ref fclsvCaLamViecThu5);
            LoadCaLamViec(label6, ref fclsvCaLamViecThu6);
            LoadCaLamViec(label7, ref fclsvCaLamViecThu7);
            LoadCaLamViec(label8, ref fclsvCaLamViecThu8);
        }

        

        private void fclsvCaLamViecThu3_ItemChecked_1(object sender, EventArgs e)
        {
            SaveCaLamViec(ref fclsvCaLamViecThu3, label3);
        }




       

       

       


    }
}
